package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.dao.AdditionalFlowInvestorDao;
import com.qdlc.p2p.dal.dto.AdditionalFlowInvestor;
import com.qdlc.p2p.dal.model.FlowInvestorModel;
import com.qdlc.p2p.dal.model.FlowProjectModel;

/**
 * 参与理财投资用户DAO接口
 * @author wjx
 * @version 2.0
 * @since 2015年05月15日 11时32分50秒
 */
@Repository(value = "additionalFlowInvestorDao")
public class AdditionalFlowInvestorDaoImpl extends BaseDaoImpl<AdditionalFlowInvestor> implements AdditionalFlowInvestorDao {

    @SuppressWarnings("unchecked")
    @Override
    public AdditionalFlowInvestor getAdditionalFlowInvestorById(long id) {
        String jpql = "from AdditionalFlowInvestor where id = ?1";
        Query query = em.createQuery(jpql);
        query.setParameter(1, id);
        List<AdditionalFlowInvestor> list = query.getResultList();
        if (list != null && list.size() >= 0) {
            return (AdditionalFlowInvestor) list.get(0);
        } else {
            return null;
        }
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<AdditionalFlowInvestor> getFlowInvestor(long pid) {
		 String jpql = "from AdditionalFlowInvestor where flowProject.id = ?1";
	        Query query = em.createQuery(jpql);
	        query.setParameter(1, pid);
	        List<AdditionalFlowInvestor> list = query.getResultList();
	        if (null !=list) {
				return list;
			}
		return null;
	}

	@Override
	public PageDataList<FlowInvestorModel> getFlowInvestorModel(long pid,
			int page, int size) {
		QueryParam param = QueryParam.getInstance();
		param.addParam("flowProject.id", pid);
		param.addOrder(OrderType.DESC, "addTime");
		if (size != 0) {
	        param.addPage(page, size);
		} else {
		    param.addPage(page);
		}
		PageDataList<AdditionalFlowInvestor> inverstor = findPageList(param);
		PageDataList<FlowInvestorModel> pageInverstor = new PageDataList<FlowInvestorModel>();
		List<FlowInvestorModel> listInverstor = new ArrayList<FlowInvestorModel>();
		for (AdditionalFlowInvestor investor : inverstor.getList()) {
			FlowInvestorModel model = FlowInvestorModel.instance(investor);
			model.setFlowProject(null);
			String username = investor.getUser().getUserName();
			model.setUserName(username.charAt(0)+"******"+username.charAt(username.length()-1));
			
			listInverstor.add(model);
		}
		pageInverstor.setList(listInverstor);
		pageInverstor.setPage(inverstor.getPage());
		return pageInverstor;
	}

	@Override
	public PageDataList<FlowInvestorModel> getUserTenderInvest(long userId,
			FlowProjectModel model) {
		QueryParam param = QueryParam.getInstance();
		param.addParam("user.userId", userId);
		param.addOrder(OrderType.DESC, "addTime");
		int size = model.getRows();
		int page = model.getPage();
		if (model.getQueryStatus() !=99) {
			param.addParam("status", model.getStatus());
		}
		if (size != 0) {
	        param.addPage(page, size);
		} else {
		    param.addPage(page);
		}
		PageDataList<AdditionalFlowInvestor> list = findPageList(param);
		PageDataList<FlowInvestorModel> pageInverstor = new PageDataList<FlowInvestorModel>();
		List<FlowInvestorModel> listInverstor = new ArrayList<FlowInvestorModel>();
		for (AdditionalFlowInvestor investor : list.getList()) {
			FlowInvestorModel projectmodel = FlowInvestorModel.instance(investor);
			projectmodel.setUserName(investor.getUser().getUserName());
			projectmodel.setProjectName(investor.getFlowProject().getName());
			projectmodel.setUuid(investor.getFlowProject().getUuid());
			projectmodel.setApr(investor.getFlowProject().getApr());
			listInverstor.add(projectmodel);
		}
		pageInverstor.setPage(list.getPage());
		pageInverstor.setList(listInverstor);
		return pageInverstor;
	}

	@SuppressWarnings("unchecked")
	@Override
	public AdditionalFlowInvestor getFlowInvestorByNo(String orderNo) {
		String jpql = "from AdditionalFlowInvestor where tppTenderOrder = ?1";
		Query query = em.createQuery(jpql);
		query.setParameter(1, orderNo);
		List<AdditionalFlowInvestor> list =query.getResultList();
		if (null !=list && list.size()>0) {
			return list.get(0);
		}
		return null;
	}
    
    
}
